import json
import urllib

from django.shortcuts import render
from django.http import JsonResponse,HttpResponse,Http404
from django.views import  View


from aqours.models import Aq_anime,Anime_Sp

# class video(View):
#
#     def get(self,request,name):
#         '''
#         aqours/v_id
#         us/v_is
#         在 anime 中进行ID 查询
#
#         第一次请求把URL发到网页
#         然后网页使用json 请求发送 到后台进行查询 然后发送到js
#
#         '''
#         anime = Anime_Sp.objects.filter(is_delete=False,sp_name=name).first()
#
#         return render(request,'videoplay/video.html',{'data':anime})
#



class video(View):
    def get(self,request,name=None,cass=None,):
        '''
        ajax 传递过来名称 然后进行查询 并发送 js
        /anime/
        /sp/

        '''
        print(name,cass)
        if name == None:
            return Http404

        else:
            if cass =="sp":
                anime = Anime_Sp.objects.filter( anime_name=name).first()

            if cass =='anime':
                anime = Aq_anime.objects.filter( anime_name=name).first()

        anime_data = {
            'anime_id':anime.id,
            'anime_name':anime.anime_name,
            'anime_class':cass,
        }
        print(anime_data)
        return render(request, 'videoplay/video.html', {'data': anime_data})


class video_play(View):
    '''

    进行接受参数准备 开始播放

    '''
    def get(self,request,name,cass):

        print(name,cass,'aaaa','sssssssssssssssssss')

        if name == None:
            return Http404

        else:
            if cass =="sp":
                anime = Anime_Sp.objects.filter( anime_name=name).first()

            if cass =='anime':
                anime = Aq_anime.objects.filter( anime_name=name).first()

        anime_data = {
            'anime_url':anime.anime_video,
        }
        print(anime_data)
        return JsonResponse(anime_data)